/*
 * Copyright (c) 2018-2999 湖南洛云网络科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 */

package com.yami.shop.live.platform.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.enums.LiveInterfaceType;
import com.yami.shop.bean.model.Product;
import com.yami.shop.common.annotation.SysLog;
import com.yami.shop.common.config.Constant;
import com.yami.shop.common.i18n.I18nMessage;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.live.comment.model.LiveProdStore;
import com.yami.shop.live.comment.param.LiveUsableNumParam;
import com.yami.shop.live.comment.service.LiveLogService;
import com.yami.shop.live.comment.service.LiveProdStoreService;
import com.yami.shop.live.comment.service.LiveRoomProdService;
import com.yami.shop.live.comment.util.WxImageUtil;
import com.yami.shop.service.ProductService;
import lombok.AllArgsConstructor;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;


/**
 *
 *
 * @author LGH
 * @date 2020-08-04 16:57:17
 */
@RestController
@AllArgsConstructor
@RequestMapping("/platform/live/liveProdStore" )
public class LiveProdStoreController {

    private final LiveProdStoreService liveProdStoreService;
    private final LiveLogService liveLogService;
    private final ProductService productService;
    private final WxImageUtil wxImageUtil;
    private final LiveRoomProdService liveRoomProdService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param liveProdStore
     * @return 分页数据
     */
    @GetMapping("/page" )
    public ResponseEntity<IPage<LiveProdStore>> getLiveProdStorePage(PageParam<LiveProdStore> page, LiveProdStore liveProdStore) {
        IPage<LiveProdStore> resPage = liveProdStoreService.getPage(page, liveProdStore);
        return ResponseEntity.ok(resPage);
    }


    /**
     * 通过id查询
     * @param liveProdStoreId id
     * @return 单个数据
     */
    @GetMapping("/info/{liveProdStoreId}" )
    public ResponseEntity<LiveProdStore> getById(@PathVariable("liveProdStoreId") Long liveProdStoreId) {
        LiveProdStore liveProdStore = liveProdStoreService.getById(liveProdStoreId);
        if (Objects.nonNull(liveProdStore.getProdId())) {
            Product product = productService.getProductByProdId(liveProdStore.getProdId(), I18nMessage.getDbLang());
            if (product !=null) {
                liveProdStore.setPic(product.getPic());
                liveProdStore.setProdName(product.getProdName());
                liveProdStore.setOriProdType(product.getProdType());
            }
        }
        return ResponseEntity.ok(liveProdStore);
    }


    /**
     * 通过id删除
     * @param liveProdStoreId id
     * @return 是否删除成功
     */
    @SysLog("删除" )
    @DeleteMapping("/{liveProdStoreId}" )
    public ResponseEntity<Boolean> removeById(@PathVariable Long liveProdStoreId) throws WxErrorException {
        liveProdStoreService.removeWxLiveProdById(liveProdStoreId,Constant.PLATFORM_SHOP_ID);
        return ResponseEntity.ok().build();
    }


    /**
     * 查询商家今日剩余可用添加直播商品、可删除商品库商品次数
     */
    @GetMapping("/getAddProdNum" )
    public ResponseEntity<LiveUsableNumParam> getAddRoomNum() {
        LiveUsableNumParam liveNum = liveLogService.getLiveNum(Constant.PLATFORM_SHOP_ID, LiveInterfaceType.ADD_PROD_AUDIT);
        LiveUsableNumParam deleteLiveNum = liveLogService.getLiveNum(Constant.PLATFORM_SHOP_ID, LiveInterfaceType.DELETE_PROD);
        liveNum.setDeleteShopNum(deleteLiveNum.getShopNum());
        liveNum.setDeleteTotalNum(deleteLiveNum.getTotalNum());
        return ResponseEntity.ok(liveNum);
    }


}
